package Dao;





import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import entity.CommentVO;
import util.DBManager;
import util.Dbconnect;

public class CommentDAOimpl implements CommentDAO{
    /**
	* Title: insert
	* Description: 选定评论类型发表评论
	* @author 邢磊
	 */

    public void insert(String email,String wordname,String type,String contant) throws SQLException {
        Connection conn=null;
        PreparedStatement statement=null;
            conn= DBManager.getConnection();
            System.out.println(contant);
            switch(type) {
			case "collocation":{
				System.out.println("collocation-");
		            String sql_insert="insert into Comment(wordname,email,collocation,thumbs,unthumbs) values(?,?,?,0,0)";
		            
		            statement=conn.prepareStatement(sql_insert);
		            
		            statement.setString(1,wordname);
		            statement.setString(2,email);
		            statement.setString(3,contant);

		            statement.executeUpdate();
		            DBManager.close(statement, conn);
				
				break;
			}
			case "example":{
				System.out.println("example-");
				
				 String sql_insert="insert into Comment(wordname,email,example,thumbs,unthumbs) values(?,?,?,0,0)";
		            
		            statement=conn.prepareStatement(sql_insert);
		            
		            statement.setString(1,wordname);
		            statement.setString(2,email);
		            statement.setString(3,contant);

		            statement.executeUpdate();
		            DBManager.close(statement, conn);
				break;
			}
			case "evaluate":{
				System.out.println("evaluate-");
				 String sql_insert="insert into Comment(wordname,email,evaluate,thumbs,unthumbs) values(?,?,?,0,0)";
		            
		            statement=conn.prepareStatement(sql_insert);
		            
		            statement.setString(1,wordname);
		            statement.setString(2,email);
		            statement.setString(3,contant);

		            statement.executeUpdate();
		            DBManager.close(statement, conn);
				break;
			}
			case "0":{
				System.out.println("请选择评论类型");
				break;
			}
		}
       
}
 /**
	* Title: delete
	* Description: 撤回评论
	* @author 邢磊
	 */

       

    public void delete(String email) {
        Connection conn=null;
        PreparedStatement statement=null;
        try {
            conn=DBManager.getConnection();
            String sql_delete="delete from Comment where email=?";
            statement=conn.prepareStatement(sql_delete);
            statement.setString(1,email);

            statement.executeUpdate();
            

        } catch (Exception e) {
            
            e.printStackTrace();

        }finally {
            DBManager.close(statement, conn);
        }
}
 /**
	* Title: update
	* Description: 更新评论
	* @author 邢磊
	 */

    
    public void update(String email,CommentVO xl) {
        Connection conn=null;
        PreparedStatement statement=null;
        try {
            conn=DBManager.getConnection();
            String sql="update Commment set wordname=?,collocation=?,example=?,evaluate=?where email=?";
            statement=conn.prepareStatement(sql);
            statement.setString(1,xl.getWordname());
            statement.setString(2,xl.getCollocation());
            statement.setString(3,xl.getExample());
            statement.setString(4,xl.getEvaluate());
            statement.setString(5,email);

            statement.executeUpdate();

           
            DBManager.close(statement, conn);
        } catch (Exception e) {
            
            e.printStackTrace();

        }finally {
            DBManager.close(statement, conn);
        }
}
 /**
	* Title: select
	* Description: 查看评论
	* @author 邢磊
	 */

    public List<CommentVO> select() {
    	List<CommentVO> xlist=new ArrayList<CommentVO>();
        Connection conn=null;
        PreparedStatement statement=null;
        try {

            conn=DBManager.getConnection();

            String sql="select * from commment";
            statement=conn.prepareStatement(sql);

            ResultSet set=statement.executeQuery();
            while(set.next()) {
               CommentVO c=new CommentVO(set.getInt("CommentID"),set.getString("email"), set.getString("wordname"),
                        set.getString("collocation"),set.getString("example"),set.getString("evaluate "),set.getInt("thumbs"),set.getInt("unthumbs"));
               xlist.add(c);
            }

            DBManager.close(statement, conn);
        } catch (Exception e) {
            e.printStackTrace();

        }finally {

            DBManager.close(statement, conn);
        }
        return xlist;
}
 /**
	* Title: addlike
	* Description: 点赞评论
	* @author 邢磊
	 */

    public void addlike(int CommentID) {
		// TODO Auto-generated method stub
		String sql = "update Comment set thumbs=thumbs+1 where CommentID=?";
		PreparedStatement st;
		try {
			st = Dbconnect.init(sql);
			st.setInt(1,CommentID);
			System.out.println(st);
			Dbconnect.update(st);
			Dbconnect.close(st);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
 /**
	* Title: addhate
	* Description: 踩扁评论
	* @author 邢磊
	 */

    public void addhate(int CommentID){
		// TODO Auto-generated method stub
		String sql = "update comment set unthumbs=unthumbs+1 where CommentID=?";
		PreparedStatement st;
		try {
			st = Dbconnect.init(sql);
			st.setInt(1,CommentID);
			System.out.println(st);
			Dbconnect.update(st);
			Dbconnect.close(st);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


    }